﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using TCA_User.Common;

namespace TCA_User
{
    public partial class Bill_List : SQL_APPL
    {
        DataTable UserInfo;
        TCA_User.Common.UserInfo_struct User_Info = new TCA_User.Common.UserInfo_struct();

        public Bill_List(DataTable user_info)
        {
            InitializeComponent();
            UserInfo = user_info.Copy();

            User_Info.UserName = ((string)UserInfo.Rows[0]["UserName"]).Trim();
            User_Info.Password = ((string)UserInfo.Rows[0]["Password"]).Trim();
            User_Info.Empl_ID = UserInfo.Rows[0]["Empl_ID"] == null ? "" : UserInfo.Rows[0]["Empl_ID"].ToString().Trim();
            User_Info.Department = UserInfo.Rows[0]["DEPARTMEMT_NAME"] == null ? "" : UserInfo.Rows[0]["DEPARTMEMT_NAME"].ToString().Trim();
            User_Info.MailAddr = UserInfo.Rows[0]["Mail"] == null ? "" : UserInfo.Rows[0]["Mail"].ToString().Trim();
            User_Info.Permission = ((string)UserInfo.Rows[0]["Permission"]).Trim();
            User_Info.Group = ((string)UserInfo.Rows[0]["Group_ID"]).Trim();
            Bill_Type.SelectedIndex = 0;
            
            // Load Card Ownerd
            Load_CardOwner();
            BillTab_CardOwnerList.DataSource = BillTab_CardOwner_Tbl;
            BillTab_CardOwnerList.DisplayMember = "UserName";
            BillTab_CardOwnerList.ValueMember = "UserName";

            Load_AddNew_Bill();
            Load_Card_List();
            Load_bill_List();
            Create_Report_Table();
            if (User_Info.Permission != ADMIN_LVL)
            {
                this.dataGridView_View.AllowUserToDeleteRows = false;
                this.dataGridView_View.AllowUserToAddRows = false;
                VS_Import_BT.Visible = false;
                ML_Import_BT.Visible = false;
                BillNo_txt.Enabled = false;
                if (User_Info.Permission == MANAGER_LVL)
                {
                    BillTab_ShowAllCardOwner_Check.Visible = true;
                    BillTab_CardOwnerList.Visible = true;
                    Bill_Tab_AllCardList.Visible = false;
                    BillTab_ShowAllCard_Check.Visible = false;
                }
                else
                {
                    BillTab_ShowAllCardOwner_Check.Visible = false;
                    BillTab_CardOwnerList.Visible = false;
                    Bill_Tab_AllCardList.Visible = false;
                    BillTab_ShowAllCard_Check.Visible = false;
                }
            }
            else 
            {
                this.dataGridView_View.AllowUserToDeleteRows = true;
                this.dataGridView_View.AllowUserToAddRows = true;
                VS_Import_BT.Visible = true;
                ML_Import_BT.Visible = true;
                Update_User_BT.Visible = true;

                // Load Tab for Card & account Manage
                Init_CommonInfo_Tab();
                Init_Taxi_Manage_Tab();
                Init_CardAlias_Tab();
                Init_Card_Borrow_Tab();
            }

            Update_BT_Status();
        }

        private void Update_Size(object sender, EventArgs e)
        {
            if (User_Info.Permission == ADMIN_LVL)
            {
                PosSize possize = new PosSize();
                possize.pos_x = 6;
                possize.pos_y = 6;
                // Update Size 
                possize.width = MainTabControl.Size.Width - 430;
                possize.height = Taxi_Manage_Tab.Size.Height - 10;
                Card_Table_Form.Update_Size(possize);
                possize.width = MainTabControl.Size.Width - 430;
                possize.height = Card_Borrow_Tab.Size.Height - 10;
                Card_Borrow_Tbl.Update_Size(possize);
            }
        }

        private void ChangPassToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TCA_User.Setting.ChangePass Setting_form = new TCA_User.Setting.ChangePass(User_Info);
            Setting_form.Update_Configure_Setting = new TCA_User.Setting.ChangePass.GetInfo(GetInfo);
            Setting_form.Show();
            //Thread ChangePass_Thread = new Thread(run_ChangePass);
            //ChangePass_Thread.Start();
        }

        private void GetInfo(UserInfo_struct user_Info)
        {
            User_Info = user_Info;
        }

        private void CardNo_CheckCorrectValue(object sender, EventArgs e)
        {
            string card_no = CardNo_comboBox.Text.Trim();

            if (Check_ItemExistTable(card_no, Card_List_Each_Provider_dtb, "Card_Provider_Name") == false)
            {
                MessageBox.Show("Can't insert new Card ID", "Warning");
                if (CardNo_comboBox.Items.Count != 0)
                {
                    CardNo_comboBox.SelectedIndex = 0;
                }
                else
                {
                    CardNo_comboBox.Text = "";
                }
            }
        }

        private void Taxi_CheckCorrectValue(object sender, EventArgs e)
        {
            string taxi = Taxi_comboBox.Text.Trim();
            if (Check_ItemExistTable(taxi, Taxi_List_dtb, "Taxi_Provider_Name") == false)
            {
                MessageBox.Show("Can't insert new Taxi", "Warning");
                Taxi_comboBox.SelectedIndex = 0;
            }
        }


    }
}
